#include <stack>
#include <iomanip>
#include <cstdlib>
#include <complex>
#include <complex.h>
#include <sstream>

using namespace std;

stack<char> opt;     //操作符栈
stack<double> val;   //操作数栈

//求阶乘所用数组
int LargeNumberFactorial[1000] = { 0 };

//定义两个常量表示在数字中的状态
const int IN = 0;
const int OUT = 1;

char opt_set[11] = "+-*/()=.^%";

int level(char theOpt);									//操作符的优先级
bool in_set(char theChar);								//判断输入的操作符是否合法，仅包括()+-*/
bool del_space(string &theString);						//用于去除空格并检查是否有非法字符
string to_string(int theInt);							//把数字转换成字符串
bool change(string &from, string &to);					//将中缀表达式转换成后缀表达式
bool compute(string &theExp);							//计算后缀表达式
bool Judge_factorial(string& theStr);					//判断是否为求阶乘
int cal_factorial(int theInt);							//利用数组求阶乘
int cal_factorial_number(string& theStr);				// 求阶乘所求的数字
bool Judge_cloth(string &str);                          //判断是否为幂运算 
int cloth_cover(double theInt,double theOut);           //求幂运算所求的数字 
bool Judge_quyu(string &theStr);                        //判断是否为取余 
int cloth_quyu(double theInt,double theOut);            //求取余后的结果 


int main()
{		
	cout << "欢迎使用计算器" << endl;
	cout << "你可以使用这个计算器做一些简单的运算" << endl;
	cout << "如果你想退出，输入:exit即可..." << endl;
	while (true) {

		//输入表达式	
		string init_exp;
 		cout << "---请输入表达式:";
		cin >> init_exp;

		//判断是否要退出
		if (init_exp == string("exit")) {
			cout << "感谢您的使用..." << endl;
			break;
		}
		

		//求阶乘的情况
		else if (Judge_factorial(init_exp)) {
			int n = cal_factorial_number(init_exp);
			if(init_exp[1]='-')
			{
				cout<<"输入有误"<<endl; 
			 } else{
			 	cout << "结果为: ";
			   for (int i = cal_factorial(n); i > 0; i--)
				cout << LargeNumberFactorial[i];
			   cout << endl;
			 }
			
		}
		
		//求幂运算的情况
		else if(Judge_cloth(init_exp)){
			double left =0.0;
			double right =0.0;
			char c;
			stringstream ss;
			ss<<init_exp;
			ss>> left >> c >> right;
			int res =	cloth_cover(left,right);
			cout<<"结果为："<<res<<endl;
		}
		
		//是取余的情况 
		else if(Judge_quyu(init_exp)){
			int  left =0;
			int right =0;
			char c;
			stringstream ss;
			ss<<init_exp;
			ss>> left >> c>> right; 
			int res = cloth_quyu(left,right);
			cout<<"结果为："<<res<<endl;
		}

		//是加减乘除的情况
		else{
			cin.clear();
			cin.sync();
			while (!opt.empty()) {
				opt.pop();
			}
			while (!val.empty()) {
				val.pop();
			}
			del_space(init_exp);//去除空格
			string cng_exp;
			cng_exp.clear();
			change(init_exp, cng_exp);	//转换为后缀表达式
			cout<<cng_exp<<endl;
			compute(cng_exp);//计算后缀表达式
			double stdans = val.top();
			cout << "结果为:" << (double)stdans << endl;
		}
	}
	return 0;
}


/*为每一个操作符返回一个数，数代表了优先级*/
int level(char theOpt)
{
	switch (theOpt) {
	case '(':
		return 0;
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	case ')':
		return 3;
	}
}


/*判断输入的操作符是否合法*/
bool in_set(char theChar) {
	for (int i = 0; i < 8; i++) {
		if (theChar == opt_set[i])
			return true;
	}
	return false;
}


/*用于去除空格并检查是否有非法字符*/
bool del_space(string& theString)
{
	string res;
	for (int i = 0; i < theString.length(); i++) {
		if (in_set(theString[i]) || isdigit(theString[i]))
			res += theString[i];
		else if (theString[i] == ' ') {}
		else{
			cout << "The enter is Wrong" << endl;
			return false;
		}
	}
	theString = res;
	return true;
}


/*把数字转换成字符串*/
string to_string(int theInt)
{
	if (theInt == 0)
		return string("0");
	bool neg = false;
	if (theInt < 0)
	{
		neg = true;
		theInt = -theInt;
	}
	string res;
	while (theInt != 0)
	{
		char c = (theInt % 10) + '0';
		res = c + res;
		theInt /= 10;
	}
	if (neg)
		res = '-' + res;
	return res;
}


/*将中缀表达式转换成后缀表达式*/
bool change(string &from, string &to)
{
	int theInt = 0;
	int state = OUT;
	char c;

	for (int i = 0; i < from.length(); i++)
	{
		c = from[i];
		if (isdigit(c)) {
			to = to + c;
			theInt *= 10;
			theInt += c - '0';
			state = IN; //状态改为在数字内
		}
		else{
			if (state == IN && c == '.') {
				to = to + '.';
				theInt = 0;
				continue;
			}
			if (state == IN && c != '.') {
				to +=  ' ';
				theInt = 0;
			}
			if (c == '=')
				break;
			else if (c == '(')
				opt.push(c);
			else if (c == ')') {
				while (!opt.empty() && opt.top() != '(') {
					to += opt.top();
					to += ' ';
					opt.pop();
				}
				opt.pop();
			}
			else {
				while (true) {
					if (opt.empty() || opt.top() == '(')
						opt.push(c);
					else if (level(c) > level(opt.top()))
						opt.push(c);
					else {
						to += opt.top();
						to += ' ';
						opt.pop();
						continue;
					}
					break;
				}
			}
			state = OUT; //状态为在数字外
		}
	}
	while (!opt.empty()) {
		to += opt.top();
		to += ' ';
		opt.pop();
	}
	return true;
}


/*计算后缀表达式*/
bool compute(string &theExp)
{
	int theInt = 0; //暂存数字
	int state = OUT;//初始状态为在数字外
	char c;
	bool dot = false;
	double count = 1.0;
	for (int i = 0; i < theExp.length(); i++)
	{
		c = theExp[i];
		if (isdigit(c)|| c == '.') {
			if (isdigit(c)) {
				theInt *= 10;
				theInt += c - '0';
				state = IN; //状态为在数字内
				if (dot == true)
					count *= 10.0;
			}
			if (c == '.') {
				dot = true;
				continue;
			}
		}
		else{
			dot = false;
			double ans = theInt / count;			
			count = 1.0;
			if (state == IN) {
				val.push(ans);
				theInt = 0;
			}
			double x, y;
			if (c != ' ') {
				x = val.top();val.pop();
				y = val.top();val.pop();

				switch (c) {
				case '+':val.push(x + y); break;
				case '-':val.push(y - x); break;
				case '*':val.push(x * y); break;
				case '/':val.push(y / x); break;
				default:cout << "未知的错误!" << endl;
				}
			}
			state = OUT;
		}
	}
	return true;
}


/*判断是否为求阶乘*/
bool Judge_factorial(string& theStr)
{
	int len = theStr.length();
	for (int i = 0; i < len; i++)
		if (theStr[i] == '!')
			return true;
	return false;
}


/*求阶乘所求的数字*/
int cal_factorial_number(string& theStr)
{
	int number = 0;
	for (int i = 0; theStr[i] != '!'; i++)
	{
		if (isdigit(theStr[i])) {
			number *= 10;
			number += theStr[i] - '0';
		}
	}
	return  number;
}


/*利用数组求阶乘*/
int cal_factorial(int theInt)
{
	LargeNumberFactorial[0] = 1;
	LargeNumberFactorial[1] = 1;
	int digit = 1;
	for (int i = 1; i <= theInt; i++)
	{
		for (int j = 1; j <= digit; j++)
		{
			LargeNumberFactorial[j] *= i;
		}
		for (int j = 1; j <= digit; j++)
		{
			if (LargeNumberFactorial[j] > 10)
			{
				for (int k = 1; k <= digit; k++)
				{
					if (LargeNumberFactorial[digit] > 9)
						digit++;
					LargeNumberFactorial[k + 1] += LargeNumberFactorial[k] / 10;
					LargeNumberFactorial[k] %= 10;
				}
			}
		}
	}
	return digit;
}


/*判断是否为幂运算*/
bool Judge_cloth(string &theStr)
{
	int len = theStr.length();
	for (int i = 0; i < len; i++)
	{
		if (theStr[i] == '^')
		{
			
			return true;
		}
	}
		
			
	return false;
}


/*求幂运算所求的数字*/ 
int cloth_cover(double theInt,double theOut){
	int cloth=0;
	cloth = pow(theInt,theOut);
	return pow(theInt,theOut);
}


/*判断是否为取余*/ 
bool Judge_quyu(string &theStr)
{
	int len = theStr.length();
	for (int i = 0; i < len; i++)
	{
		if (theStr[i] == '%')
		{
			
			return true;
		}
	}
		
			
	return false;
}


/*求取余后的结果 */ 
int cloth_quyu(double theInt,double theOut){
	if((int)theInt<(int)theOut){
		return theInt;
	}
	int a =theInt/theOut;
	int b = theInt-theOut*a;
	return b;
}
